/*****
jDatepicker v0.0.5
*****/

(function(){
	$.fn.extend({
		jDatepicker: function(callback) {

			var defaults = {
				bg_clr 	: '#000'
			};
			var options = $.extend(defaults);
			var $this = $(this);
			var date = new Date();
			var html = '<div id="jdper"><div id="jdper-ctrl"><div id="jdper-prev"><</div><div id="jdper-year"></div><div id="jdper-month"></div><div id="jdper-next">></div></div><div id="jdper-weeks"><div class="jdper-week">日</div><div class="jdper-week">一</div><div class="jdper-week">二</div><div class="jdper-week">三</div><div class="jdper-week">四</div><div class="jdper-week">五</div><div class="jdper-week">六</div></div><div id="jdper-days"><!--<div class="jdper-empty"></div><div class="jdper-day">1</div>--></div><div id="jdper-action"><div id="jdper-cancel">取消</div><div id="jdper-clear">清空</div></div></div>';

			//初始化
			function init()
			{
				var year = date.getFullYear();
				var month = date.getMonth();
				var day = date.getDate();
				
				$this.unbind('mousedown').bind('mousedown', function(){
					$('#jdper').remove();
					$this = $(this);
					draw(year, month);
				});
			}

			function draw(year, month)
			{
				//设置年份，月份
				date.setFullYear(year, month, 1);
				var week = date.getDay();
				var days = get_days(year, month);
				var $jdper = $(html);
				//绘制空格
				for(i=0; i<week; i++)
				{
					$jdper.find('#jdper-days').append('<div class="jdper-empty"></div>');
				}
				//绘制日期
				for(i=0; i<days; i++)
				{
					day = i + 1;
					$jdper.find('#jdper-days').append('<div class="jdper-day">' + day + '</div>');
				}
				//绘制年份，月份
				$jdper.find('#jdper-year').text(year);
				$jdper.find('#jdper-month').text(month + 1);
				//定位
				var pos = get_pos();
				$jdper.css({'top':pos[0],'left':pos[1]});
				//绘制日历
				if ($('#jdper').length == 1)
				{
					$('#jdper').remove();
				}
				$('body').append($jdper);
				//绑定新事件
				select_day();
				select_month(year, month);
				action();
			}

			function select_day()
			{
				$('.jdper-day').unbind('mousedown');
				$('.jdper-day').bind('mousedown', function(){
					var date = $('#jdper-year').text() + '-' + $('#jdper-month').text() + '-' + $(this).text();
					$this.val(date);
					$('#jdper').remove();
				});
			}

			function is_leap(year)
			{
				if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0))
				{
					return true;
				}
				else
				{
					return false;
				}
			}

			function get_pos()
			{
				var top = $this.offset().top + $this.outerHeight();
				var left = $this.offset().left;
				return [top, left];
			}

			function get_days(year, month)
			{
				var large_months = [0, 2, 4, 6, 7, 9, 11];
				var small_months = [3, 5, 8, 10];

				for (i=0; i<small_months.length; i++)
				{
					if (month == small_months[i])
					{
						return 30;
					}
				}

				for (i=0; i<large_months.length; i++)
				{
					if (month == large_months[i])
					{
						return 31;
					}
				}

				if (is_leap(year))
				{
					return 29;
				}
				else
				{
					return 28;
				}

				return false;
			}

			function select_month(year, month)
			{
				var year = year;
				var month = month;
				$('#jdper-next').unbind('click')
				.bind('click', function(){
					month = month + 1;
					if (month > 11)
					{
						month = 0;
						year = year + 1;
					}
					draw(year, month);
				});
				$('#jdper-prev').unbind('click')
				.bind('click', function(){
					month = month - 1;
					if (month < 0)
					{
						month = 11;
						year = year - 1;
					}
					draw(year, month);
				});
			}

			function action()
			{
				$('#jdper-cancel').unbind('click').bind('click', function(){
					$('#jdper').remove();
				});
				$('#jdper-clear').unbind('click').bind('click', function(){
					$('#jdper').remove();
					$this.val('');
				});
			}

			init();

		}
	})
})(jQuery);